package demo.bytebuddy;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * description: <br>
 * createTime: 2019/2/209:57 <br>
 *
 * @author zzh
 */
public class ByteBuddyDemo2 {

    public static void main(String[] args) {
        Foo foo = new Foo();
        System.out.println(foo.m() + "\t\t\t" + Foo.s() + "\t\t\t" + foo.l);
        System.out.println("------------------");
        ByteBuddyAgent.install();
        new ByteBuddy()
                .redefine(Bar.class)
                .name(Foo.class.getName())
                .make()
                .load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
        assertThat(foo.m(), is("bar"));
        System.out.println(foo.m() + "\t\t\t" + Foo.s() + "\t\t\t" + foo.l);
        System.out.println(new Foo().m() + "\t\t\t" + Foo.s() + "\t\t\t" + new Foo().l);
    }
}